package com.bit101.components {
	import com.bit101.components.Interface.ITextFormat;
	
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;

	/**
	 * 多行文本框 
	 */	
	[Event(name="change",type="flash.events.Event")]
	public class Text extends Component implements ITextFormat{
		protected var _tf:TextField;
		protected var _text:String = "";
		protected var _editable:Boolean = true;
		protected var _selectable:Boolean = true;
		protected var _html:Boolean = false;
		protected var _format:TextFormat;
		protected var _panel:Panel;

		public function Text(parent:DisplayObjectContainer = null, xpos:Number = 0, ypos:Number = 0, text:String = ""){
			this.text = text;
			super(parent, xpos, ypos);
			setSize(200, 100);
		}

		/**
		 * 初始化
		 */
		override protected function init():void {
			super.init();
		}

		/**
		 * 创建及添加子显示对象
		 */
		override protected function addChildren():void {
			_panel = new Panel(this);
			_panel.color = Style.TEXT_BACKGROUND;
			
			_format = new TextFormat(Style.fontName, Style.fontSize, Style.LABEL_TEXT);
			_tf = new TextField();
			_tf.x =_tf.y = 2;
			_tf.height = _height;
			_tf.embedFonts = Style.embedFonts;
			_tf.multiline  = true;
			_tf.wordWrap   = true;
			_tf.selectable = true;
			_tf.type = TextFieldType.INPUT;
			_tf.defaultTextFormat = _format;
			_tf.addEventListener(Event.CHANGE, onChange);
			this.addChild(_tf);
		}

		/**
		 * Draws the visual ui of the component.
		 */
		override public function draw():void {
			super.draw();
			_panel.setSize(_width, _height);
			_panel.draw();
			
			_tf.width  = _width - 4;
			_tf.height = _height - 4;
			
			if (_html){     //html格式
				_tf.htmlText = _text;
			} else {
				_tf.text 	 = _text;
			}
			if (_editable){ //非编辑
				_tf.mouseEnabled = true;
				_tf.selectable = true;
				_tf.type       = TextFieldType.INPUT;
			} else {
				_tf.mouseEnabled = _selectable;
				_tf.selectable   = _selectable;
				_tf.type 		 = TextFieldType.DYNAMIC;
			}
			_tf.setTextFormat(_format);
		}
		
		public function setFontSize(size:uint,color:uint=0x333333):void{
			_format.size = size;
			_format.color= color;
		}
		public function setTextFormat(format:TextFormat):void{
			_tf.setTextFormat(format);
		}

		/**
		 * Called when the text in the text field is manually changed.
		 */
		protected function onChange(event:Event):void {
			_text = _tf.text;
			dispatchEvent(event);
		}

		///////////////////////////////////
		// getter/setters
		///////////////////////////////////
		/**
		 * Gets / sets the text of this Label.
		 */
		public function set text(t:String):void {
			_text = t;
			if (_text == null)
				_text = "";
			invalidate();
		}

		public function get text():String {
			return _text;
		}

		/**
		 * Returns a reference to the internal text field in the component.
		 */
		public function get textField():TextField {
			return _tf;
		}

		/**
		 * Gets / sets whether or not this text component will be editable.
		 */
		public function set editable(b:Boolean):void {
			_editable = b;
			invalidate();
		}

		public function get editable():Boolean {
			return _editable;
		}

		/**
		 * Gets / sets whether or not this text component will be selectable. Only meaningful if editable is false.
		 */
		public function set selectable(b:Boolean):void {
			_selectable = b;
			invalidate();
		}

		public function get selectable():Boolean {
			return _selectable;
		}

		/**
		 * Gets / sets whether or not text will be rendered as HTML or plain text.
		 */
		public function set html(b:Boolean):void {
			_html = b;
			invalidate();
		}

		public function get html():Boolean {
			return _html;
		}

		/**
		 * Sets/gets whether this component is enabled or not.
		 */
		public override function set enabled(value:Boolean):void {
			super.enabled = value;
			_tf.tabEnabled = value;
		}
	}
}